const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            project_category: [],
            government_agencies: [],
            natural_resources_management: [],
            government_member_information: [],
            dataForm: {
                Emergency_Sign: 0,
            },
            Natural_Resource_SerialNumber: [],
            childDataForm1: [],//地块
            childDataForm2: [],//附件
            childDataForm3: [{}],
            funds_source:[],
            fileList: [],
            showDialog: false,
            fileUrl: '',
            type:'0'
        };
    },
    mounted() {
        let ls_URL = window.location.href;
        this.type = getQueryObject(ls_URL, "type");// 获取 nodeKey 参数
        this.getCategory();
    },
    watch: {
        'dataForm.Project_Implementation_Subject'(val) {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: "POST",
                data: {
                    SQLSerialNumber: 'f_government_member_information',
                    Condition: window.btoa(unescape(encodeURIComponent(""))),
                    ExtensionCondition: window.btoa(unescape(encodeURIComponent("AND government_member_information.Government_Agencies_SerialNumber = '" + val + "'"))),
                },
                success: (data) => {
                    this.government_member_information = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        childDataForm1: {
            handler(newVal, oldVal) {
                let arr = [];
                newVal.forEach(item => {
                    arr.push(item.Natural_Resource_SerialNumber);
                })
                this.Natural_Resource_SerialNumber = arr;
            },
            deep: true // 深度监听
        },
    },
    methods: {
        getCategory() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'project_category',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.project_category = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'funds_source',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.funds_source = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: "POST",
                data: {
                    SQLSerialNumber: 't_natural_resources_registration',
                    Condition: window.btoa(unescape(encodeURIComponent("%%,%%,%%"))),
                    ExtensionCondition: '',
                },
                success: (data) => {
                    this.natural_resources_management = data; // 直接使用 this
                }, complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                type: "GET",
                data: {
                    TableName: 'government_agencies',
                    NodeCode: "",
                },
                success: (data) => {
                    this.government_agencies = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        success(row, res) {
            this.childDataForm2.push({
                uid: row.uid,
                Project_SerialNumber: this.dataForm.Project_SerialNumber,
                Attachment_URL: res.fileName,
                Attachment_Name: row.name,
                Temp_Path: res.tempPath,
                Attachment_Type: '1'
            })
        },
        remove(row) {
            if (!Object.keys(row).some(item => item.includes('_Original'))) {
                this.childDataForm2 = this.childDataForm2.filter(i => i.uid != row.uid);
            } else {
                let obj = this.childDataForm2.filter(i => i.Attachment_SerialNumber == row.Attachment_SerialNumber)[0];
                obj.Row_Status = '1'
            }
        },
        preview(row) {
            this.fileUrl = photo + row.Attachment_URL;
            this.showDialog = true;
        },
        async download(row) {
            // if (row.Attachment_URL) {
            //     window.location.href = photo + row.Attachment_URL;
            // }
            let apiUrl = photo + row.Attachment_URL;
            let fileName = apiUrl.split('/').pop();
            try {
                const response = await fetch(apiUrl);
                const blob = await response.blob();
                const url = window.URL.createObjectURL(blob);

                const a = document.createElement('a');
                a.href = url;
                a.download = fileName || 'download';
                document.body.appendChild(a);
                a.click();

                // 清理
                setTimeout(() => {
                    document.body.removeChild(a);
                    window.URL.revokeObjectURL(url);
                }, 100);
            } catch (error) {
                console.error('下载失败:', error);
            }
        },
        async handleSubmit(is) {
            if (is == '2') {
                this.$confirm('此操作将审核通过该项目申报,是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }).then(() => {
                    this.dataForm.Project_Status = is;
                    let lJSONObject_DataSet = {
                        Modify:{
                            project:[this.dataForm] 
                        },
                        Add : {
                            project_examine_log:[{
                                Project_SerialNumber:this.dataForm.Project_SerialNumber,
                                Row_Status:'0'
                            }]
                        }
                    }
                    let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                    lJSONObject_DataSet['Add']['base_operation_log'] = [{
                        Operation_Type: '0',
                        Operation_Path: this.relativePath,
                        Operation_Main,
                        Operation_Interface: "generalSaveByRequestBody",
                        Operation_IP: getIpAddress()
                    }]
                    submitSave(lJSONObject_DataSet, (res) => {
                        if (res.Status == 'success') {
                            window.location.href = './index.html'
                        }
                        this.loading = false;
                    })
                    // $.ajax({
                    //     headers: requestHeaders("JSON"),
                    //     url: `${mh}/generalBusiness/generalSaveByRequestBody`,
                    //     method: "post",
                    //     data: JSON.stringify({
                    //         Data: lJSONObject_DataSet,
                    //     }),
                    //     success: (res) => {
                    //         if (res.Status == 'success') {
                    //             window.location.href = './index.html'
                    //         }
                    //     },
                    //     complete: (XMLHttpRequest) => {
                    //         this.loading = false;
                    //         requestPretreatment(XMLHttpRequest)
                    //     }
                    // });
                }).catch(() => { });
            } else if (is == '3') {
                this.$prompt("请输入拒绝原因", '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    inputPattern: /^\s*\S.+$/,
                    inputErrorMessage: "请输入拒绝原因"
                }).then(({value}) => {
                    this.dataForm.Project_Status = is;
                    let lJSONObject_DataSet = {
                        Modify:{
                            project:[this.dataForm] 
                        },
                        Add : {
                            project_examine_log:[{
                                Project_SerialNumber:this.dataForm.Project_SerialNumber,
                                Reason:value,
                                Row_Status:'1'
                            }]
                        }
                    }
                    let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                    lJSONObject_DataSet['Add']['base_operation_log'] = [{
                        Operation_Type: '0',
                        Operation_Path: this.relativePath,
                        Operation_Main,
                        Operation_Interface: "generalSaveByRequestBody",
                        Operation_IP: getIpAddress()
                    }]
                    submitSave(lJSONObject_DataSet, (res) => {
                        if (res.Status == 'success') {
                            window.location.href = './index.html'
                        }
                        this.loading = false;
                    })
                    // $.ajax({
                    //     headers: requestHeaders("JSON"),
                    //     url: `${mh}/generalBusiness/generalSaveByRequestBody`,
                    //     method: "post",
                    //     data: JSON.stringify({
                    //         Data: lJSONObject_DataSet,
                    //     }),
                    //     success: (res) => {
                    //         if (res.Status == 'success') {
                    //             window.location.href = './index.html'
                    //         }
                    //     },
                    //     complete: (XMLHttpRequest) => {
                    //         this.loading = false;
                    //         requestPretreatment(XMLHttpRequest)
                    //     }
                    // });
                }).catch(() => { });
            }
        },
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-cascader', ylCascader);
app.component('yl-select', ylSelect);
app.component('yl-online-preview', ylOnlinePreview);
app.component('yl-text-editor', ylTextEditor);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
